package com.cyan.note.mapper;

import com.cyan.note.entity.Customer;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 客户管理Mapper接口
 * 
 * @author ZhangzhenYu
 * @since 2025-01-15
 */
@Mapper
public interface CustomerMapper {

    /**
     * 分页查询客户列表
     */
    List<Customer> selectCustomerPage(
            @Param("shopId") Long shopId,
            @Param("nickname") String nickname,
            @Param("status") List<String> status,
            @Param("level") List<String> level,
            @Param("registerStart") LocalDateTime registerStart,
            @Param("registerEnd") LocalDateTime registerEnd,
            @Param("minAmount") BigDecimal minAmount,
            @Param("maxAmount") BigDecimal maxAmount,
            @Param("minOrders") Integer minOrders,
            @Param("maxOrders") Integer maxOrders,
            @Param("offset") Integer offset,
            @Param("limit") Integer limit
    );

    /**
     * 查询客户总数
     */
    int countCustomers(
            @Param("shopId") Long shopId,
            @Param("nickname") String nickname,
            @Param("status") List<String> status,
            @Param("level") List<String> level,
            @Param("registerStart") LocalDateTime registerStart,
            @Param("registerEnd") LocalDateTime registerEnd,
            @Param("minAmount") BigDecimal minAmount,
            @Param("maxAmount") BigDecimal maxAmount,
            @Param("minOrders") Integer minOrders,
            @Param("maxOrders") Integer maxOrders
    );

    /**
     * 根据ID查询客户
     */
    Customer selectById(@Param("id") Long id);

    /**
     * 插入客户
     */
    int insert(Customer customer);

    /**
     * 更新客户
     */
    int updateById(Customer customer);

    /**
     * 删除客户
     */
    int deleteById(@Param("id") Long id);

    /**
     * 根据昵称和店铺ID查询客户（用于客户校验）
     */
    Customer selectByNicknameAndShopId(@Param("nickname") String nickname, @Param("shopId") Long shopId);

    /**
     * 统计客户数据概览
     */
    CustomerStatsResult selectCustomerStats(@Param("shopId") Long shopId);

    /**
     * 更新客户累计消费和订单数
     */
    int updateCustomerConsumption(
            @Param("customerId") Long customerId,
            @Param("addConsumption") BigDecimal addConsumption,
            @Param("addOrders") Integer addOrders
    );

    /**
     * 根据ID和店铺ID查询客户
     */
    Customer selectByIdAndShop(@Param("id") Long id, @Param("shopId") Long shopId);

    /**
     * 根据手机号和店铺ID查询客户
     */
    Customer selectByPhoneAndShop(@Param("phone") String phone, @Param("shopId") Long shopId);

    /**
     * 客户统计结果
     */
    class CustomerStatsResult {
        private Integer totalCount;
        private Integer activeCount;
        private Integer newCount;
        private Integer vipCount;
        private Integer debtCount;

        // getters and setters
        public Integer getTotalCount() { return totalCount; }
        public void setTotalCount(Integer totalCount) { this.totalCount = totalCount; }
        public Integer getActiveCount() { return activeCount; }
        public void setActiveCount(Integer activeCount) { this.activeCount = activeCount; }
        public Integer getNewCount() { return newCount; }
        public void setNewCount(Integer newCount) { this.newCount = newCount; }
        public Integer getVipCount() { return vipCount; }
        public void setVipCount(Integer vipCount) { this.vipCount = vipCount; }
        public Integer getDebtCount() { return debtCount; }
        public void setDebtCount(Integer debtCount) { this.debtCount = debtCount; }
    }
}